RUNTIME = mono --debug
CSCOMPILE = mcs -debug+
CS2COMPILE = gmcs -debug+

UCD_TABLE = downloaded/UnicodeData.txt
UCA_TABLE = downloaded/allkeys.txt
DERIV_CORE_PROP_TABLE = downloaded/DerivedCoreProperties.txt
DERIV_AGE_TABLE = downloaded/DerivedAge.txt
NORM_TABLE = downloaded/DerivedNormalizationProps.txt
SCRIPTS_TABLE = downloaded/Scripts.txt
CB_CLASS_TABLE = downloaded/DerivedCombiningClass.txt
CP932_TABLE = downloaded/CP932.TXT
UCA_DATA = downloaded/core.zip
NORM_TEST_TABLE = downloaded/NormalizationTest.txt

NORMGEN_FILENAME = create-normalization-source
COLL_ELEM_FILENAME = create-collation-element-table
MSTABLEGEN = create-mscompat-collation-table
MSCOMPAT_FILENAME = MSCompatUnicodeTableGenerated.cs
NORM_FILENAME = NormalizationGenerated.cs
NORM_TEST = StringNormalizationTest
NORM_TESTGEN = create-normalization-tests

all : collation normalization

test : norm-test

normalization: $(NORM_FILENAME)

collation : $(MSCOMPAT_FILENAME)

norm-test : $(NORM_TEST_TABLE) $(NORM_TEST).cs
	$(CS2COMPILE) -unsafe $(NORM_TEST).cs $(NORM_FILENAME) NormalizationTableUtil.cs CodePointIndexer.cs -d:TEST_STANDALONE
	$(RUNTIME) $(NORM_TEST).exe

distclean :
	$(MAKE) clean
	rm $(UCA_TABLE) $(NORM_TABLE) $(CB_CLASS_TABLE) $(CP932_TABLE) $(DERIV_CORE_PROP_TABLE) $(SCRIPTS_TABLE) $(NORM_TEST_TABLE)

clean : clean-collation clean-normalization clean-mscompat clean-normtest

clean-collation :
	rm -f CollationElementTable.cs $(COLL_ELEM_FILENAME).exe* ../resources/collation.*.bin

clean-normalization :
	rm -f $(NORM_FILENAME) $(NORMGEN_FILENAME).exe*

clean-mscompat :
	rm -f $(MSCOMPAT_FILENAME) $(MSTABLEGEN).exe*

clean-normtest :
	rm -f $(NORM_TEST).* $(NORM_TESTGEN).exe*

# collation element table
CollationElementTable.cs : $(COLL_ELEM_FILENAME).exe CollationElementTable.template $(UCA_TABLE)
	echo "// WARNING: Do not edit this file. It is autogenerated." > CollationElementTable.cs
	cat CollationElementTable.template >> CollationElementTable.cs || rm CollationElementTable.cs
	MONO_DISABLE_MANAGED_COLLATION=yes $(RUNTIME) $(COLL_ELEM_FILENAME).exe <  $(UCA_TABLE) >> CollationElementTable.cs || rm CollationElementTable.cs
	echo "	}" >> CollationElementTable.cs
	echo "}" >> CollationElementTable.cs

COLL_TABLE_GENERATOR_SOURCES = \
	$(COLL_ELEM_FILENAME).cs \
	CollationElementTableUtil.cs \
	CodePointIndexer.cs

$(COLL_ELEM_FILENAME).exe : $(COLL_TABLE_GENERATOR_SOURCES)
	MONO_DISABLE_MANAGED_COLLATION=yes $(CSCOMPILE) $(COLL_TABLE_GENERATOR_SOURCES)

# normalization (including combining class)
$(NORM_FILENAME) : $(NORMGEN_FILENAME).exe Normalization.cs $(NORM_TABLE) $(UCD_TABLE) $(CB_CLASS_TABLE)
	echo "// WARNING: Do not edit this file. It is autogenerated." > $(NORM_FILENAME)
	echo "#define GENERATE_TABLE" >> $(NORM_FILENAME); \
		cat Normalization.cs >> $(NORM_FILENAME); \
		$(RUNTIME) $(NORMGEN_FILENAME).exe >> $(NORM_FILENAME); \
		echo "	}" >> $(NORM_FILENAME); \
		echo "}" >> $(NORM_FILENAME) || rm $(NORM_FILENAME)

NORM_TABLE_GENERATOR_SOURCES = \
	$(NORMGEN_FILENAME).cs \
	NormalizationTableUtil.cs \
	CodePointIndexer.cs

$(NORMGEN_FILENAME).exe : $(NORM_TABLE_GENERATOR_SOURCES)
	MONO_DISABLE_MANAGED_COLLATION=yes $(CSCOMPILE) $(NORM_TABLE_GENERATOR_SOURCES)

$(MSTABLEGEN).exe : $(CREATE_MSCOMPAT_TABLE_SOURCES)
	MONO_DISABLE_MANAGED_COLLATION=yes $(CSCOMPILE) $(CREATE_MSCOMPAT_TABLE_SOURCES)

CREATE_MSCOMPAT_TABLE_SOURCES = \
	create-mscompat-collation-table.cs \
	CodePointIndexer.cs \
	CollationElementTable.cs \
	CollationElementTableUtil.cs \
	MSCompatUnicodeTableUtil.cs

#wgets

$(UCD_TABLE) :
	wget www.unicode.org/Public/UNIDATA/UnicodeData.txt
	mv UnicodeData.txt downloaded/
	touch downloaded/UnicodeData.txt

$(SCRIPTS_TABLE) :
	wget www.unicode.org/Public/UNIDATA/Scripts.txt
	mv Scripts.txt downloaded/
	touch downloaded/Scripts.txt

$(UCA_TABLE) :
	wget http://www.unicode.org/Public/UCA/latest/allkeys.txt
	mv allkeys.txt downloaded/
	touch downloaded/allkeys.txt

$(LDML_RULES) :
	wget "http://dev.icu-project.org/cgi-bin/viewcvs.cgi/*checkout*/icu/source/data/unidata/UCARules.txt?rev=HEAD&content-type=text/plain"
	mv UCARules.txt* downloaded/
	touch downloaded/UCARules.txt

$(DERIV_CORE_PROP_TABLE) :
	wget http://www.unicode.org/Public/UNIDATA/DerivedCoreProperties.txt
	mv DerivedCoreProperties.txt downloaded/
	touch downloaded/DerivedCoreProperties.txt

$(DERIV_AGE_TABLE) :
	wget http://www.unicode.org/Public/UNIDATA/DerivedAge.txt
	mv DerivedAge.txt downloaded/
	touch downloaded/DerivedAge.txt

$(NORM_TABLE) :
	wget http://www.unicode.org/Public/UNIDATA/DerivedNormalizationProps.txt
	mv DerivedNormalizationProps.txt downloaded/
	touch downloaded/DerivedNormalizationProps.txt

$(CB_CLASS_TABLE) :
	wget http://www.unicode.org/Public/UNIDATA/extracted/DerivedCombiningClass.txt
	mv DerivedCombiningClass.txt downloaded/
	touch downloaded/DerivedCombiningClass.txt

$(CP932_TABLE) :
	wget http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.TXT
	mv CP932.TXT downloaded/
	touch downloaded/CP932.TXT

$(UCA_DATA) :
	rm -f core.zip
	wget http://unicode.org/Public/cldr/1.3.0/core.zip
	rm -fr common dtd
	unzip core.zip
	rm -fr downloaded/common
	mv common downloaded
	mv core.zip downloaded
	touch downloaded/core.zip

$(NORM_TEST_TABLE) : downloaded/NormalizationTest.txt
	wget http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
	mv NormalizationTest.txt downloaded/
	touch downloaded/NormalizationTest.txt


#MSCompatUnicodeTableGenerated.cs

$(MSCOMPAT_FILENAME) : MSCompatUnicodeTable.cs CollationElementTable.cs $(MSTABLEGEN).exe $(SCRIPTS_TABLE) $(UCD_TABLE) $(UCA_TABLE) $(CP932_TABLE) $(DERIV_CORE_PROP_TABLE) $(DERIV_AGE_TABLE) $(UCA_DATA)
	echo "// WARNING: Do not edit this file. It is autogenerated." > $(MSCOMPAT_FILENAME)
	echo "#define GENERATE_TABLE" > $(MSCOMPAT_FILENAME)
	cat MSCompatUnicodeTable.cs >> $(MSCOMPAT_FILENAME)
	MONO_DISABLE_MANAGED_COLLATION=yes $(RUNTIME) $(MSTABLEGEN).exe downloaded >> $(MSCOMPAT_FILENAME)
	echo "	}" >> $(MSCOMPAT_FILENAME)
	echo "}" >> $(MSCOMPAT_FILENAME)

sample : 
	$(CSCOMPILE) normtest.cs $(NORM_FILENAME) NormalizationTableUtil.cs CodePointIndexer.cs

#StringNormalizationTest.cs

$(NORM_TEST).cs : $(NORM_TESTGEN).cs
	$(CSCOMPILE) $(NORM_TESTGEN).cs
	$(RUNTIME) ./$(NORM_TESTGEN).exe > $(NORM_TEST).cs

